package com.chenfan.mcn.enums;

import com.chenfan.common.exception.BusinessException;
import com.chenfan.mcn.exception.McnErrorCode;
import com.chenfan.mcn.model.StarRateCard;
import com.chenfan.mcn.service.ApprovalCallbackService;
import com.chenfan.mcn.service.impl.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.List;

/**
 * 审批流 processId
 *
 * @author lizhejin
 */
@AllArgsConstructor
@Getter
public enum ApprovalEnum {

    /**
     * 红人刊例管理-流程id
     */
    STAR_RATE_CARD(1475703355941584896L, StarRateCardServiceImpl.class, "红人刊例管理", "page", "advertisementManageDetail"),

    /**
     * 红人返点合同-流程id
     */
    STAR_REBATE_CONTRACT(1460432114292883456L, StarRebateContractServiceImpl.class, "红人返点合同", "page", "redsContractDetail"),

    /**
     * 折扣审批单-流程id
     */
    DISCOUNT_APPROVAL(1353956254594105344L, DiscountApprovalServiceImpl.class, "折扣审批单", "page", "discountRebatesDetail"),

    /**
     * 年框合同-流程id
     */
    YEAR_CONTRACT(1353956291122298880L, YearFrameContractServiceImpl.class, "年框合同", "page", "yearContractDetail"),

    /**
     * 年框合同补充协议-流程id
     */
    YEAR_SUPPLEMENTARY_AGREEMENT(1354997108150632448L, YearSupplementaryAgreementServiceImpl.class, "年框合同补充协议", "page", "yearContractDetail"),

    /**
     * 执行单-流程id
     */
    EXECUTION_INSIDE_STAR(1357228448099074048L, ContractExcutionOrderServiceImpl.class, "内部红人执行单", "page", "executionOrderDetail"),

    /**
     *
     */
    EXCUTION_OUTSIDE_STAR(1357228313122177024L, ContractExcutionOrderServiceImpl.class, "外部红人执行单", "page", "executionOrderDetail"),

    /**
     * 年度优惠-收入合同&散单-收入合同-流程id
     */
    INCOME_INCOME_CONTRACT(1353956411045838852L, IncomeContractServiceImpl.class, "收入合同", "page", "revenueContractDetail"),

    /**
     * 年度优惠-形式合同&散单-形式合同-流程id
     */
    INCOME_FORM_CONTRACT(1363789817275482112L, IncomeContractServiceImpl.class, "形式合同", "page", "revenueContractDetail"),

    /**
     * 直播合同-流程ID
     */
    INCOME_LIVE_CONTRACT(1426057163410767872L, IncomeContractServiceImpl.class, "直播合同", "page", "revenueContractDetail"),


    /**
     * 直播合同-流程ID
     */
    INCOME_LIVE_CONTRACT_OFFLINE_COMMISSION(1665561860608360448L, LiveContactOfflineCommissionServiceImpl.class, "直播合同-线下佣金结算", "page", "revenueContractDetail"),



    /**
     * 年度优惠-收入合同补充协议&散单-收入合同补充协议-流程id
     */
    INCOME_CONTRACT_AGREEMENT(1364854537772335104L, IncomeContactAgreementServiceImpl.class, "收入合同补充协议", "page", "revenueContractDetail"),

    /**
     * 年度优惠-形式合同补充协议&散单-形式合同补充协议-流程id
     */
    INCOME_FORM_AGREEMENT(1364854537776529408L, IncomeContactAgreementServiceImpl.class, "形式合同补充协议", "page", "revenueContractDetail"),

    /**
     * 直播合同-流程ID
     */
    INCOME_LIVE_AGREEMENT(1426057612218073088L, IncomeContactAgreementServiceImpl.class, "直播合同补充协议", "page", "revenueContractDetail"),

    /**
     * 采购合同-流程id
     */
    PURCHASE_CONTRACT(1368817135056322560L, PurchaseContractServiceImpl.class, "采购合同", "page", "procurementContractDetail"),
    /**
     * 客户返点审批单-流程id
     */
    ANNUAL_REBATE_APPROVAL(1368818709048590336L, AnnualRebateApprovalServiceImpl.class, "客户返点审批单", "page", "annualRebateApprovalDetail"),

    /**
     * 商务提成
     */
    BUSINESS_COMMISSION(1368818709048590987L, null, "商务提成", "page", "commerceCommissionTodtl"),

    /**
     * 商务提成--部门负责人提成
     */
    BUSINESS_DEPARTMENT(1368818709048590987L, null, "商务提成", "page", "commerceCommissionTodtl2"),

    /**
     * 商务提成--给抄送人发
     */
    BUSINESS_DEFAULT_COMMISSION(1368818709048590987L, null, "商务提成", "page", "commerceCommissionTodtl3"),

    /**
     * AE执行提成单
     */
    AE_COMMISSION(1368818709048590988L, null, "AE提成", "page", "aeCommissionTodtl"),

    /**
     * 部门负责人提成单
     */
    AE_DEPARTMENT(1368818709048590988L, null, "AE提成", "page", "aeCommissionTodtl2"),


    /**
     * AE执行提成单(只发给一个人)
     */
    AE_DEFAULT_COMMISSION(1368818709048590988L, null, "AE提成", "page", "aeCommissionTodtl3"),

    /**
     * 红人月度结算单-流程id
     */
    STAR_MONTHLY_SETTLEMENT(1483310373481218048L, StarMonthlySettlementServiceImpl.class, "红人月度结算单", "page", "starMonthlySettlementDetail"),

    /**
     * 直播红人结算单
     */
    LIVE_STAR_SETTLEMENT(1636664387492118528L, LiveStarSettlementServiceImpl.class, "直播红人结算单", "page", "settlementFormDetail"),

    /**
     * 前置执行单-AE主管审核
     */
    INTENTION_ORDER_AE(1769628998323519488L, DocIntentionOrderServiceImpl.class, "前置执行单AE主管审核", "page", "intentionOrderDetail"),


    ;

    /**
     * approvalTemplateId 流程那边的模板id改了名字，改数据比较麻烦 所以备注说明一下
     */
    private final Long processId;
    private final Class<? extends ApprovalCallbackService> clazz;
    private final String processName;
    private final String type;
    private final String url;

    public static Class<? extends ApprovalCallbackService> getServiceClass(Long processId) {
        ApprovalEnum[] approvalEnums = ApprovalEnum.values();
        for (ApprovalEnum approvalEnum : approvalEnums) {
            if (approvalEnum.getProcessId().equals(processId)) {
                return approvalEnum.getClazz();
            }
        }
        return null;
    }

    /**
     * 通过收入合同类型获取合同审批流枚举
     * @param contractType
     * @return
     */
    public static ApprovalEnum getEnumByContractType(Integer contractType) {
        Assert.notNull(contractType,"合同类型为空，无法获取收入合同流程枚举失败！");
        if(IncomeContractTypeEnum.isFormalContract(contractType)){
            return INCOME_FORM_CONTRACT;
        } else if(IncomeContractTypeEnum.isLiveContract(contractType)){
            return INCOME_LIVE_CONTRACT;
        }
        return INCOME_INCOME_CONTRACT;
    }

    /**
     * 通过收入合同类型获取合同补充协议审批流枚举
     * @param contractType
     * @return
     */
    public static ApprovalEnum getEnumByContractAgreementType(Integer contractType) {
        Assert.notNull(contractType,"合同类型为空，获取合同补充协议流程枚举失败！");
        if(IncomeContractTypeEnum.isFormalContract(contractType)){
            return INCOME_FORM_AGREEMENT;
        } else if(IncomeContractTypeEnum.isLiveContract(contractType)){
            return INCOME_LIVE_AGREEMENT;
        }
        return INCOME_CONTRACT_AGREEMENT;
    }

    /**
     * 通过执行单类型获取执行单审批流枚举
     * @param excutionOrderType
     * @return
     */
    public static ApprovalEnum getEnumByExcutionOrderType(Integer excutionOrderType) {
        if (ExcutionOrderTypeEnum.INSIDE_EXCUTION_ORDER.getType().equals(excutionOrderType)) {
            return ApprovalEnum.EXECUTION_INSIDE_STAR;
        } else if (ExcutionOrderTypeEnum.isOutside(excutionOrderType)) {
            return ApprovalEnum.EXCUTION_OUTSIDE_STAR;
        } else {
            throw new BusinessException(McnErrorCode.EXECUTION_ORDER_NOT_MATCH_APPROVAL);
        }
    }

    /**
     * 获取收入合同审批流程ID集合
     * @return
     */
    public static List<Long> getContractProcessIds(){
        return Arrays.asList(INCOME_INCOME_CONTRACT.getProcessId(),
                INCOME_FORM_CONTRACT.getProcessId(),
                INCOME_LIVE_CONTRACT.getProcessId());
    }

    /**
     * 获取收入合同审批流程ID集合+线下佣金
     * @return
     */
    public static List<Long> getContractProcessIdsWithOfflineCommission(){
        return Arrays.asList(INCOME_INCOME_CONTRACT.getProcessId(),
                INCOME_FORM_CONTRACT.getProcessId(),
                INCOME_LIVE_CONTRACT.getProcessId(),
                INCOME_LIVE_CONTRACT_OFFLINE_COMMISSION.getProcessId());
    }

    /**
     * 获取收入合同补充协议-审批流程ID集合
     * @return
     */
    public static List<Long> getContractAgreementProcessIds(){
        return Arrays.asList(INCOME_CONTRACT_AGREEMENT.getProcessId(),
                INCOME_FORM_AGREEMENT.getProcessId(),
                INCOME_LIVE_AGREEMENT.getProcessId());
    }

    /**
     * 获取执行单-审批流程ID集合
     * @return
     */
    public static List<Long> getExecutionOrderProcessIds(){
        return Arrays.asList(EXECUTION_INSIDE_STAR.getProcessId(),
                EXCUTION_OUTSIDE_STAR.getProcessId());
    }
}
